package com.os.start.controller;

import com.os.core.common.JsonResult;
import com.os.core.common.ResultCode;
import com.os.start.pojo.SUser;
import com.os.utils.JsonUtils;
import com.os.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author songc
 * @Date 2022-01-07
 * @Desc:
 * @Version 1.0
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisOperator redisOperator;

    @RequestMapping("test")
    public JsonResult test(){
        redisTemplate.opsForValue().set("test-redis:hello:world", "hello redis~~~");

        return JsonResult.returnResponse(true);
    }

    @RequestMapping("testTtl")
    public JsonResult testTtl(){
        redisOperator.set("test-redis:hello:world1", "hello redis~~~", 10);

        String value = redisOperator.get("test-redis:hello:world1");
        long expire = redisOperator.ttl("test-redis:hello:world1");

        return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, "jedis值为：" + value + " 剩余时间为：" + expire);

    }

    @RequestMapping("/getJsonList")
    public JsonResult getJsonList(){
        SUser user = new SUser();
        user.setId("6789");
        user.setName("你好");

        SUser user1 = new SUser();
        user1.setId("1234");
        user1.setName("哈哈");

        List<SUser> userList = new ArrayList<SUser>();
        userList.add(user);
        userList.add(user1);
        redisOperator.set("test-redis:user", JsonUtils.objectToJson(userList), 1000);

        return JsonResult.returnResponse(true);

    }
}
